package com.taobao.client.pages;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.taobao.client.HeadPanel;
import com.taobao.client.RPC;
import com.taobao.client.sdk.CloseOrderRequest;
import com.taobao.client.util.PopBox;

public class CloseOrder extends Composite {

	public CloseOrderRequest input;
	private static CloseOrder closeOrder;
	private Frame f;
	private static final String RESULT_FLAG = "ResultCode";
	public final static String HISTORY_ITEM = "CloseOrderPage";
	
	/**
	 * @param tradeId the trade id to set.
	 */
	public void setTradeId(String tradeId) {
		input.setBiz_order_id(tradeId);
		
		RPC.getService().getUrl(input.getURLMap(), inputCallBack);
	}
	
	public static CloseOrder getCloseOrder() {
		if (closeOrder == null) {
			closeOrder = new CloseOrder();
		}
		return closeOrder;
	}

	private CloseOrder() {
		String queryString = Window.Location.getQueryString();

		if (queryString != null && queryString.indexOf(RESULT_FLAG) >= 0) {
			// TODO Process the result.
		} else
			initialize();
	}

	private void initialize() {
		VerticalPanel content = new VerticalPanel();
		content.add(createHead("Close Order"));

		f = new Frame();
		content.add(f);

		initWidget(content);
		
		// Initialize the input parameters
		input = new CloseOrderRequest();
		input.initialize();	
	}

	private AsyncCallback<String> inputCallBack = new AsyncCallback<String>() {
		@Override
		public void onFailure(Throwable arg0) {
			PopBox.error("构造url出错", arg0.getMessage());
		}

		@Override
		public void onSuccess(String arg0) {
//			f.setUrl(arg0);
//			f.setSize(Data.width, Data.height);
			
			PopBox.openWindow(arg0);
		}
	};

	/**
	 * Create a head panel
	 * 
	 * @param title the text to be displayed in the head panel
	 * @return a panel that is displayed in the head part always
	 */
	private HeadPanel createHead(String title) {
		HeadPanel head = new HeadPanel();
		head.add(new Label(title));
		return head;
	}
}
